# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def mergeInBetween(self, list1: ListNode, a: int, b: int,
                       list2: ListNode) -> ListNode:
        aPoint = None
        bPoint = None
        k = 0
        node = list1
        while node:
            if k == a - 1:
                aPoint = node
            if k == b:
                bPoint = node
            node = node.next
            k += 1
        aPoint.next = list2
        lastNode = list2
        while lastNode.next:
            lastNode = lastNode.next
        lastNode.next = bPoint.next
        bPoint.next = lastNode
        return list1
